<template>
  <div>
    <!-- 间距组件 -->
    <el-space>
      <el-input v-model.trim="username" placeholder="请输入管理员名称" />
      <el-button type="primary" class="iconfont icon-sousuo" @click="seek()"
        >查找</el-button
      >
      <el-button type="primary" class="iconfont icon-jishiben" @click="add()"
        >添加</el-button
      >
    </el-space>
    <!-- 表格组件 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="管理员ID" />
      <el-table-column prop="username" label="管理员名称" />
      <el-table-column prop="realname" label="真实姓名" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column prop="roleIds" label="管理员角色">
        <template #default="scope">
          <el-tag v-for="(item, index) in scope.row.roleIds" :key="index">
            {{ opps(item) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加对话框 -->
    <el-dialog v-model="dialogVisible" title="创建" width="30%">
      <el-form
        status-icon
        :rules="rules"
        label-width="100px"
        :model="show"
        class="heads"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="show.username" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="show.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="show.phone" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="show.realname" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="show.roleIds" placeholder="请选择角色" multiple>
            <el-option
              v-for="item in rold"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="addEd()">新增</el-button>
          <el-button @click="addNnll()">重置</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑管理员 -->
    <el-dialog v-model="editor" title="修改" width="30%">
      <el-form
        ref="ruleForm"
        v-model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        class="heads"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
            type="text"
            autocomplete="off"
            disabled
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm.phone" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="ruleForm.realname" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select
            v-model="ruleForm.roleIds"
            placeholder="请选择角色"
            multiple
          >
            <el-option
              v-for="item in rold"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="Modifies()">新增</el-button>
          <el-button @click="editora()">重置</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 分页组件 -->
    <PaginAtion
      class="dins"
      v-bind="paging"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    >
    </PaginAtion>
  </div>
</template>
<script>
  import PaginAtion from "@/Subassembly/PaginAtion";
  import http from "@/http";
  import { ElMessage } from "element-plus";
  export default {
    components: {
      PaginAtion,
    },
    data() {
      return {
        username: "", //查找名称
        tableData: [], //表格数据
        dialogVisible: false, //添加对话框
        editor: false, //编辑对话框
        rold: [],
        dialogVisibless: false,
        //分页数据
        paging: {
          page: 1, //默认第一页
          limit: 10, //显示一行几页
          total: "", //总页
        },
        //修改
        ruleForm: {
          id: "",
          username: "",
          realname: "",
          phone: "",
          password: "",
          roleIds: [],
        },
        id: "",
        value: "",
        //添加
        show: {
          username: "",
          password: "",
          phone: "",
          realname: "",
          roleIds: [],
        },
      };
    },
    created() {
      this.careTaker();
      this.appe();
    },
    methods: {
      //查找
      seek() {
        if (this.username == "") {
          return ElMessage.error("查找管理员名称不能为空！");
        }
        this.careTaker();
      },
      //枚举数据接口
      appe() {
        let ass = {
          _gp: "admin.role",
          _mt: "options",
        };
        http.post(ass).then((res) => {
          console.log(res);
          this.rold = res.data;
        });
      },
      add() {
        this.dialogVisible = true;
      },
      addEd() {
        if (this.show.username == "") {
          return ElMessage.error("用户名不能为空");
        } else if (this.show.password == "") {
          return ElMessage.error("密码不能为空");
        } else if (this.show.phone == "") {
          return ElMessage.error("手机不能为空");
        } else if (this.show.realname == "") {
          return ElMessage.error("真实姓名不能为空");
        } else if (this.show.roleIds == "") {
          return ElMessage.error("角色信息不能为空");
        }

        var myreg = /^1\d{10}$/;
        if (!myreg.test(this.show.phone)) {
          return ElMessage.error("手机格式要11位！");
        }
        var reg = /^[\u4e00-\u9fa5]*$/;
        if (!reg.test(this.show.realname)) {
          return ElMessage.error("真实姓名必须是中文");
        }
        let odd = {
          _gp: "admin",
          _mt: "create",
          adminDTO: JSON.stringify(this.show),
        };

        http.post(odd).then((res) => {
          console.log(res);
          if (200 == res.errno) {
            ElMessage.success("添加管理员成功");
            this.dialogVisible = false;
            this.careTaker();
            this.show = this.$options.data().show;
          } else {
            ElMessage.error("用户名已重复");
          }
        });
      },
      opps(row) {
        for (const items of this.rold) {
          if (row == items.value) {
            return items.label;
          }
        }
      },
      addNnll() {
        this.show = {
          phone: "",
          realname: "",
          roleIds: [],
        };
      },
      editora() {
        this.ruleForm.phone = "";
        this.ruleForm.roleIds = [];
        this.ruleForm.realname = "";
        this.ruleForm.password = "";
      },

      //渲染管理员数据
      careTaker() {
        let data = {
          _gp: "admin",
          _mt: "list",
          username: this.username,
          page: this.paging.page,
          limit: this.paging.limit,
        };
        http.get(data).then((res) => {
          console.log(res);
          this.paging.total = res.data.total;
          this.tableData = res.data.items;
        });
      },
      //修改管理员
      handleEdit(index, row) {
        this.editor = true;
        this.ruleForm.id = row.id;
        this.ruleForm.username = row.username;
        this.ruleForm.realname = row.realname;
        this.ruleForm.phone = row.phone;
        this.ruleForm.roleIds = row.roleIds;
      },
      //修改
      Modifies() {
        if (this.ruleForm.password == "") {
          return ElMessage.error("密码不能为空");
        } else if (this.ruleForm.phone == "") {
          return ElMessage.error("手机不能为空");
        } else if (this.ruleForm.realname == "") {
          return ElMessage.error("真实姓名不能为空");
        } else if (this.ruleForm.roleIds == "") {
          return ElMessage.error("角色信息不能为空");
        }
        var phones = /^1\d{10}$/;
        if (!phones.test(this.ruleForm.phone)) {
          return ElMessage.error("手机格式要11位！");
        }
        var regs = /^[\u4e00-\u9fa5]*$/;
        if (!regs.test(this.ruleForm.realname)) {
          return ElMessage.error("真实姓名必须是中文");
        }
        let bianji = {
          _gp: "admin",
          _mt: "edit",
          adminDTO: JSON.stringify(this.ruleForm),
        };
        http.post(bianji).then((res) => {
          console.log(res);
          if (200 == res.errno) {
            ElMessage.success("修改成功");
            this.editor = false;
            this.careTaker();
          }
        });
      },
      //删除管理员
      handleDelete(index, row) {
        console.log(row);
        ElMessageBox.confirm("是否删除此管理员", {
          confirmButtonText: "OK",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let deletes = {
              _gp: "admin",
              _mt: "delete",
              id: row.id,
            };
            http.post(deletes).then((res) => {
              console.log(res);
              if (200 == res.errno) {
                this.tableData.splice(index, 1);
              }
            });
            ElMessage({
              type: "success",
              message: "删除成功",
            });
          })
          .catch(() => {
            ElMessage({
              type: "info",
              message: "取消成功",
            });
          });
      },
      //分页组件
      handleSizeChange(val) {
        this.paging.limit = val;
        this.careTaker();
      },
      handleCurrentChange(val) {
        this.paging.page = val;
        this.careTaker();
      },
    },
  };
</script>
<style lang="less" scoped>
  // .el-space {
  //   height: 50px;
  //   margin-left: 9px;
  // }
  /deep/ .el-form-item__label {
    width: 60px;
  }
  /deep/.el-popper {
    width: 180px;
  }
  /deep/ .el-input__inner {
    width: 140px;
  }
  .heads {
    /deep/ .el-input__inner {
      width: 240px;
    }
  }
  //   .dins {
  //     margin-top: 10px;
  //   }
  /deep/ .btn-prev {
    margin-left: 30px;
  }
  // /deep/.el-pager{
  //     margin-left: 30px;
  // }
</style>
